﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace FinalTestMachine.Motion.Aixs
{
    /// <summary>
    /// 旋转轴移动到下个工位的帮助类
    /// </summary>
    public static class RotatedMovNextHelper
    {
        #region 270°转盘
        //A,B,C,D:对应转盘位置，逆时针转
        //转盘初始等待点为A
        //只要知道其中的，一个位置，就能知道其它的位置
        //初始时：A->0度；B->90度；C->180度；D->270度
        //每90度移位一次：1：ABCD(0度);2:DABC（90度）;3:CDAB（180度）;4:BCDA（270度）;5:ABCD
        private static readonly string[] pos_tag = new string[4] { "A", "B", "C", "D" };

        /// <summary>
        /// 用于存储从工位一到工位三治具号
        /// </summary>
        public static string[] tagPos { get; private set; } = new string[Aix_Turn.StationNum];

        public static double StationAngle = Aix_Turn.PerRad;
        //ADCB

        /// <summary>
        /// 转盘位置是否正确标识
        /// </summary>
        public static bool IsOkPos { get; private set; }

        static bool IsFinished = false;

        public static RotateDir FixtureRotateDir { get; private set; } = RotateDir.AntiClockWise;

        /// <summary>
        /// 扫描检测当前主轴的位置
        /// </summary>
        public static void ScanCheckMainPos()
        {
            IsFinished = false;
            IsOkPos = false;
            int dValue = 2;

            for (int i = 0; i < GlobalParams.WorkstationSum; i++) {
                if (judgeCurrentPos(i, dValue))
                {
                    IsOkPos = true;
                    return;
                }
            }
            
            IsFinished = true;
        }

        /// <summary>
        /// 判断当前旋转轴在哪个位置
        /// </summary>
        /// <param name="index">第几个站位，这里默认索引从0开始</param>
        /// <param name="tolerance">误差</param>
        /// <returns></returns>
        public static bool judgeCurrentPos(int index, int tolerance)
        {
            //计算出第n个位置的数值
            double angle = Aix_Turn.Cur.WaitPos.PosMsg.GetValue + StationAngle * index;
            if(Math.Abs(Aix_Turn.Cur.GetEncPos() - angle) <= tolerance)
            //if (Aix_Turn.Cur.GetEncPos() < angle + tolerance && Aix_Turn.Cur.GetEncPos() > angle - tolerance)
            {
                //为每一站位刷新当前治具号
                for (int i = 0; i < tagPos.Length; i++)
                {
                    switch (FixtureRotateDir)
                    {
                        case RotateDir.AntiClockWise:
                            tagPos[index] = ((char)(65 + i)).ToString();
                            index++;
                            if (index >= tagPos.Length)
                            {
                                index = 0;
                            }
                            break;

                        case RotateDir.ClockWise:
                            tagPos[index] = ((char)(65 + i)).ToString();
                            index--;
                            if (index < 0)
                            {
                                index = tagPos.Length;
                            }
                            break;

                        default: break;
                    }
                    
                }
                return true;
            }
            return false;
        }

        //转盘不可控状态3秒,确保转盘位置正确
        public static void Ref_Wait()
        {
            for (int i = 0; i < 2; i++)
            {
                IsFinished = false;
                int tm = 30;
                while (!IsFinished && tm-- > 0)
                {
                    Thread.Sleep(10);
                }
            }

        }

        /// <summary>
        /// 位置索引
        /// </summary>
        public static int Index { get; private set; }


        public static bool MovNext()
        {
            return Aix_Turn.Cur.Mov_xPos(Index);
        }

        /// <summary>
        /// 重置索引
        /// </summary>
        public static void Reset()
        {
            Index = 0;
        }

        /// <summary>
        /// 设置索引
        /// </summary>
        public static void SetIndex(int it)
        {
            if (it >= GlobalParams.WorkstationSum)
            {
                Index = 0;
            }
            else
            {
                Index = it;
            }
        }
        #endregion

    }

}
